<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of wallet_model
 *
 * @author win7
 */
class wallet_model extends admin_model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'wallet';
        $this->select = array('user.username' ,'wallet.* ');
        $this->order = array($this->table.'.id' => metohd_get_uri('sort', 'desc'));
        $this->join = array('user' => 'user.id=wallet.user_id');
        $this->columns_op = array(
            'user_id' => 'Callback_get_userid_with_username',
            'create_date >=' => 'Callback_get_date'
        );
    }

    public function sfilter($nolimit = false) {
        /* ======== filter ======== */


        if (metohd_get_uri('username')) {
            $userid = get_userid_with_username(metohd_get_uri('username'));
            $this->db->where_in('wallet.user_id', $userid);
        }
        if (metohd_get_uri('id')) {

            $id = explode(',', metohd_get_uri('id'));

            $this->db->where_in('wallet.id', $id);
        }
        // status
        if (metohd_get_uri('status')) {
            $status = explode(',', metohd_get_uri('status'));
            $this->db->where_in('wallet.status', $status);
        }
        // operation
        if (metohd_get_uri('operation')) {
            $this->db->like('wallet.operation', metohd_get_uri('operation'));
        }
        // amount
        if (metohd_get_uri('amount')) {
            $this->db->like('wallet.amount', metohd_get_uri('amount'));
        }

        // Create_date
        if (metohd_get_uri('create_date')) {
            $date = date('Y-m-d H:i:s', strtotime(metohd_get_uri('create_date')));
            $this->db->where('wallet.create_date >=', $date);
        }




        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT );


        $statrt_limit = get_segment(4, 0);
        if ($nolimit == false) {
            $this->db->limit($limit, $statrt_limit);
        }



        $sql = $this->db->get();

        return $sql->result();
    }

    public function confirm($data) {
        $update = array();
        $bank_discount = get_discount_bank();
        $update['status'] = 'active';
        $update['user_update'] = $this->admin_id;
        $out = metohd_get_uri('out');
        if (!empty($out)) {
            $update['discount'] = $bank_discount;
            $update['amount'] = ($data->amount - $bank_discount);
            $update['note'] = $data->note . ' ' . str_replace('%s', $bank_discount, lang('message_wallet_discount_15'));
        }
        $this->db->update('wallet', $update, array('id' => $data->id));
        //$this->email_system->SendSingelEmail('', $data->user_id, 'wallet_confirm', 'تم تاكيد  طلبكم الخاص بالمحفظة', $data = array('{ID}'=>$data->id));
    }

    public function rejected($id) {
        $update = array();
        $update['status'] = 'rejected';
        $update['admin_note'] = $this->input->post('note');
        $update['user_update'] = $this->admin_id;
        if (is_array($id)) {
            $this->db->where_in('id', $id);
        } else {
            $this->db->where('id', $id);
        }

        $this->db->update('wallet', $update, array());
    }

}

?>
